package com.tyk.bookstore.back.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.excel.ExcelManager;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.common.core.mvc.annotation.UnderlineToCamelModel;
import com.tyk.bookstore.back.system.manager.excel.constant.ExcelConstant;
import com.tyk.bookstore.back.system.manager.excel.model.PermissionExcelVo;
import com.tyk.bookstore.back.system.model.query.PermissionAddQuery;
import com.tyk.bookstore.back.system.model.query.PermissionPageQuery;
import com.tyk.bookstore.back.system.model.query.PermissionUpdateQuery;
import com.tyk.bookstore.back.system.model.vo.PermissionPageVo;
import com.tyk.bookstore.back.system.model.vo.PermissionSuggestionVo;
import com.tyk.bookstore.back.system.model.vo.PermissionVo;
import com.tyk.bookstore.back.system.service.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "系统管理/权限管理", description = "权限管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/system/permission")
public class PermissionController {

    private final PermissionService permissionService;
    private final ExcelManager<PermissionExcelVo> excelManager;

    @Operation(summary = "分页查询权限列表", description = "分页查询权限列表")
    @SaCheckPermission("system:permission:page")
    @GetMapping("page")
    public R<PermissionPageVo> getPage(
            @Validated @UnderlineToCamelModel PermissionPageQuery query) {
        return R.ok(permissionService.getPage(query));
    }

    @Operation(summary = "根据ID获取权限信息", description = "根据ID获取权限信息")
    @SaCheckPermission("system:permission:get")
    @GetMapping("{id}")
    public R<PermissionVo> getPermissionById(
            @Schema(description = "权限ID")
            @Validated @Min(value = 1)
            @PathVariable("id") Long id
    ) {
        return R.ok(permissionService.getById(id));
    }

    @Operation(summary = "新增权限", description = "新增权限")
    @SaCheckPermission("system:permission:add")
    @PostMapping("")
    public R<Object> add(@Validated @RequestBody PermissionAddQuery query) {
        permissionService.add(query);
        return R.ok();
    }

    @Operation(summary = "根据ID修改权限信息", description = "根据ID修改权限信息")
    @SaCheckPermission("system:permission:update")
    @PutMapping("")
    public R<Object> updateById(@Validated @RequestBody PermissionUpdateQuery query) {
        permissionService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "根据ID删除权限信息", description = "根据ID删除权限信息")
    @SaCheckPermission("system:permission:delete")
    @DeleteMapping("{id}")
    public R<Object> deletePermissionById(
            @Schema(description = "权限ID")
            @Validated @Min(value = 1)
            @PathVariable("id") Long id
    ) {
        permissionService.deleteById(id);
        return R.ok();
    }

    @Operation(summary = "查询权限建议列表", description = "查询权限建议列表")
    @SaCheckPermission({"system:permission:suggestion:list"})
    @GetMapping("suggestion/list")
    public R<List<PermissionSuggestionVo>> getSuggestionList() {
        return R.ok(permissionService.getSuggestionList());
    }

    @Operation(summary = "导出Excel", description = "导出Excel")
    @SaCheckPermission("system:permission:excel:download")
    @GetMapping("excel")
    public void downloadExcel(PermissionPageQuery query, HttpServletResponse resp) {
        excelManager.write(
                permissionService.getPermissionExcelVoList(query),
                ExcelConstant.PERMISSION_FILE_NAME,
                ExcelConstant.PERMISSION_SHEET,
                resp
        );
    }

    @Operation(summary = "导入Excel", description = "导入Excel")
    @SaCheckPermission("system:permission:excel:upload")
    @PostMapping("excel")
    public R<Object> uploadExcel(MultipartFile file) {
        excelManager.save(
                file,
                permissionService::savePermissionExcelVoList
        );
        return R.ok();
    }


    @Operation(summary = "下载Excel模板", description = "下载Excel模板")
//    @SaCheckPermission("system:permission:excel:template")
    @GetMapping("excel/template")
    public void excelTemplate(HttpServletResponse resp) {
        excelManager.template(
                ExcelConstant.PERMISSION_TEMPLATE,
                List.of("名称", "权限字符串", "描述", "分组"),
                resp
        );
    }

}
